<!DOCTYPE html>
<!--
Copyright 2011 Seth Ladd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
  <head>
    <meta charset="utf-8">
    <title>Box2D Javascript Fun : 00 : Most Basic Box2D Example</title>
    <link rel="author" href="http://profiles.google.com/sethladd">
    <script src="../Prueba_Conjunta_Box2D_Three/js/box2d/Box2d.min.js"></script>
    <script src="../Prueba_Conjunta_Box2D_Three/js/stats.js"></script>
   
   <!-- <script type="text/javascript">
      (function() {
        var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
        po.src = 'https://apis.google.com/js/plusone.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
      })();
    </script>-->
    
  </head>
  <body>
    
    <canvas id="c" width="640" height="480" style="border: 1px solid black"></canvas>
  
    <script>
    // http://paulirish.com/2011/requestanimationframe-for-smart-animating/
    window.requestAnimFrame = (function(){
          return  window.requestAnimationFrame       || 
                  window.webkitRequestAnimationFrame || 
                  window.mozRequestAnimationFrame    || 
                  window.oRequestAnimationFrame      || 
                  window.msRequestAnimationFrame     || 
                  function(/* function */ callback, /* DOMElement */ element){
                    window.setTimeout(callback, 1000 / 60);
                  };
    })();
    
    var stats = new Stats();
    document.body.appendChild(stats.domElement);
    
    var canvas = document.getElementById("c");
    var ctx = canvas.getContext("2d");
  
    var world;
  
    function init() {
       var   b2Vec2 = Box2D.Common.Math.b2Vec2
        , b2BodyDef = Box2D.Dynamics.b2BodyDef
        , b2Body = Box2D.Dynamics.b2Body
        , b2FixtureDef = Box2D.Dynamics.b2FixtureDef
        , b2Fixture = Box2D.Dynamics.b2Fixture
        , b2World = Box2D.Dynamics.b2World
        , b2MassData = Box2D.Collision.Shapes.b2MassData
        , b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
        , b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
        , b2DebugDraw = Box2D.Dynamics.b2DebugDraw
          ;
     
       world = new b2World(
             new b2Vec2(0, 10)    //gravity
          ,  true                 //allow sleep
       );
       
       var SCALE = 30;
     
       var fixDef = new b2FixtureDef;
       fixDef.density = 1.0;
       fixDef.friction = 0.5;
       fixDef.restitution = 0.3;
     
       var bodyDef = new b2BodyDef;
     
       //create ground
       bodyDef.type = b2Body.b2_staticBody;
       // positions the center of the object (not upper left!)
       bodyDef.position.x = canvas.width / 2 / SCALE;
       bodyDef.position.y = (canvas.height / SCALE) - 1;
       fixDef.shape = new b2PolygonShape;
       // half width, half height. eg actual height here is 1 unit
       fixDef.shape.SetAsBox((canvas.width / SCALE) / 2, 0.5 / 2);
       world.CreateBody(bodyDef).CreateFixture(fixDef); // Creamos el cuerpo y lo introducimos
     
       
       // create dynamic polygon object
       bodyDef.type = b2Body.b2_dynamicBody;
       fixDef.shape = new b2PolygonShape;
       fixDef.shape.SetAsBox(50 / SCALE ,50 / SCALE);
       bodyDef.position.x =canvas.width / 2 / SCALE;
       bodyDef.position.y = 0;
       world.CreateBody(bodyDef).CreateFixture(fixDef);
     
       //setup debug draw
       var debugDraw = new b2DebugDraw();
       debugDraw.SetSprite(document.getElementById("c").getContext("2d"));
       debugDraw.SetDrawScale(SCALE);
       debugDraw.SetFillAlpha(0.3);
       debugDraw.SetLineThickness(1.0);
       debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
       world.SetDebugDraw(debugDraw);
     
       // restart
       setTimeout(init, 8000);
    }; // init()
  
    function update() {
       world.Step(
             1 / 60   //frame-rate
          ,  10       //velocity iterations
          ,  10       //position iterations
       );
       world.DrawDebugData();
       world.ClearForces();
     
       stats.update();
       requestAnimFrame(update);
    }; // update()
  
  
  
    init();
    requestAnimFrame(update);
    
    </script>
    
    <div class="g-plusone" data-size="small" data-annotation="inline"></div>
  </body>
</html>